
package com.zjxd.business.web.api.login;

import java.util.Date;

import org.springframework.stereotype.Service;

import com.zjxd.business.web.code.RedisCode;
import com.zjxd.business.web.common.ApiResult;
import com.zjxd.business.web.common.BaseService;
import com.zjxd.business.web.model.pojo.TAUser;
import com.zjxd.business.web.utils.CoreUtil;
import com.zjxd.business.web.utils.MD5Utils;

@Service
public class LoginUserServiceImpl extends BaseService implements LoginUserService {

	/**
	 * 登录验证使用
	 */
	public ApiResult getLoginUserByMap(LoginModel model) {

		if (CoreUtil.isBlank(model.getUserName())) { return ApiResult.fail("请输入用户名"); }
		if (CoreUtil.isBlank(model.getPassword())) { return ApiResult.fail("请输入密码"); }
		TAUser tAUser = tAUserMapper.getLoginUserByMap(model.getUserName());
		if (null == tAUser) { return ApiResult.fail("用户不存在"); }
		String pwd = MD5Utils.MD5_Complex(model.getPassword());
		if (!pwd.equals(tAUser.getUserPassword())) { return ApiResult.fail("密码错误"); }
		String token = CoreUtil.getRandomString(10);
		LoginFo fo = new LoginFo();
		fo.setLoginTime(new Date());
		fo.setUserName(tAUser.getUserName());
		fo.setToken(token);
		fo.setLoninTimeLength(60 * 60 * 2L);
		redisService.setObj(RedisCode.login_name, token, fo, fo.getLoninTimeLength());
		return ApiResult.success(fo);
	}
}
